package com.ken;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "BServlet", urlPatterns = {"/Bpage"})
public class BServlet extends javax.servlet.http.HttpServlet {
    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String username = request.getParameter("username");

        System.out.println("接受到的POST请求参数 Bpage username：" + username);
        response.setContentType("text/html;charset = utf-8");
//创建cookie 对象
        Cookie cookie = new Cookie("username", username);
        //设置cookie 有效期 单位是秒
        cookie.setMaxAge(1 * 6000);
//        添加 cookie 对象
        response.addCookie(cookie);
        StringBuffer stf = new StringBuffer();

        PrintWriter out = response.getWriter();
        stf.append("<!DOCTYPE html>");
        stf.append("<html lang=\"en\">");
        stf.append("<head>");
        stf.append("<meta charset=\"UTF-8\">");
        stf.append(" <title>Cpage</title>");
        stf.append("</head>");
        stf.append("<body>");
        stf.append("<h3 >Web 保持url 状态 ，数据传递</h3>");
        stf.append(" 用户名：").append(username).append("<br>");
        stf.append("   <form method=\"POST\" action=\"Cpage\">");
        stf.append("  <input type=\"submit\" value=\"跳转到C页面\">");
        stf.append("   </form>");
//        stf.append("<a href=\"Cpage?username=").append(username).append("\">跳转到C页面</a>");
        stf.append(" </body>");
        stf.append(" </html>");

        out.print(stf.toString());
        out.close();
    }

    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {

    }
}
